﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Trademark.BO;
using AMS.DataAccess.BO;
using AMS.DataAccess.Interfaces;
using AMS.DataAccess.Trademark.Interfaces;

public partial class Cases_EditCaseContactor : BaseWorkPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Utils.ParseIntQueryString(Request, "caseid", ref caseId))
        {
            GlobalTransfer.RedirectToErrorPage("参数不正确");
            return;
        }

        // TODO: Check for owner access here

        if (!IsPostBack)
        {
            DoDataBind();
            BindPageTitle();
        }
        else
        {
            LoadPostBackData();
        }
    }

    void LoadPostBackData()
    {
        hfAddedCustoemrID.Value = Request[hfAddedCustoemrID.UniqueID];
    }

    #region Properties
    int caseId;
    protected int CaseID
    {
        get { return caseId; }
    }
    protected int CustomerID
    {
        set { ViewState["CustomerID"] = value; }
        get
        {
            if (ViewState["CustomerID"] == null)
            {
                ICaseBasicInfoManager caseMgr = ManagerFactory.CreateCaseBasicInfoManager();
                CaseBasicInfo caseInfo = caseMgr.GetCaseBasicInfo(CallContext, caseId);
                ViewState["CustomerID"] = caseInfo.CustomerID;
            }

            return ViewState["CustomerID"] == null ? 0 : (int)ViewState["CustomerID"]; }
    }
    #endregion

    void DoDataBind()
    {
        ICaseContactorManager caseContactorMgr = ManagerFactory.CreateCaseContactorManager();
        List<CaseContactorInfo> contactorList = caseContactorMgr.GetCaseContactors(CallContext, caseId);
        gvApp.DataSource = contactorList;
        gvApp.DataKeyNames = new string[] { "ContactorID" };
        gvApp.DataBind();

        lblNoRecordMsg.Visible = (contactorList == null || contactorList.Count == 0);
    }

    void BindPageTitle()
    {
        ICaseBasicInfoManager caseMgr = ManagerFactory.CreateCaseBasicInfoManager();
        CaseDisplayInfo caseInfo = caseMgr.GetCaseBasicInfo(CallContext, caseId);
        lblCaseName.Text = caseInfo.CaseIndex + ", " + caseInfo.CaseName + ", 客户名称：" + caseInfo.CustomerName;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Server.Transfer("EditCaseTrademark.aspx" + Request.Url.Query);
    }

    protected void uc_OnApplicantAdded(object sender, EventArgs e)
    {
        DoDataBind();
        ucContactor.Reset();
    }

    protected void lblEdit_Command(object sender, CommandEventArgs e)
    {
        int itemIndex = int.Parse(e.CommandArgument.ToString());
        int contactorId = Convert.ToInt32(gvApp.DataKeys[itemIndex].Value);
        ICaseContactorManager appMgr = ManagerFactory.CreateCaseContactorManager();
        CaseContactorInfo contactorInfo = appMgr.GetCaseContactor(CallContext, caseId, contactorId);
        ucContactor.EditContactor(contactorInfo);
    }

    protected void lblDelete_Command(object sender, CommandEventArgs e)
    {
        int itemIndex = int.Parse(e.CommandArgument.ToString());
        int contactorId = Convert.ToInt32(gvApp.DataKeys[itemIndex].Value);
        ICaseContactorManager appMgr = ManagerFactory.CreateCaseContactorManager();
        if (appMgr.DeleteCaseContactor(CallContext, caseId, contactorId))
        {
            MsgInfo("删除成功。");
        }

        DoDataBind();
    }

    protected void lblDeleteDetailInfo_Command(object sender, CommandEventArgs e)
    {
        int itemIndex = int.Parse(e.CommandArgument.ToString());
        int contactorId = Convert.ToInt32(gvApp.DataKeys[itemIndex].Value);
        IContactorManager customerMgr = ManagerFactory.CreateContactorManager();
        ICaseContactorManager appMgr = ManagerFactory.CreateCaseContactorManager();
        try
        {
            appMgr.DeleteCaseContactor(CallContext, caseId, contactorId);
            customerMgr.DeleteContactor(CallContext, contactorId);
            DoDataBind();
            MsgInfo("删除成功。");
        }
        catch (Exception ex)
        {
            MsgError("由于关联关系，联系人信息将不允许删除。");
            // TODO: log to the system
        }
    }

    protected void btnGetContactor_Click(object sender, EventArgs e)
    {
        int contactorId = int.Parse(hfAddedCustoemrID.Value);
        CaseContactorInfo contactorInfo = new CaseContactorInfo();
        contactorInfo.ContactorID = contactorId;
        contactorInfo.CaseID = caseId;
        if (gvApp.Rows.Count == 0)
        {
            contactorInfo.IsFirst = true;
        }

        ICaseContactorManager contactorMgr = ManagerFactory.CreateCaseContactorManager();
        CaseContactorInfo tmpInfo = contactorMgr.GetCaseContactor(CallContext, caseId, contactorId);
        if (tmpInfo == null)
        {
            contactorMgr.AddCaseContactor(CallContext, contactorInfo);
            DoDataBind();
            MsgInfo("添加成功。");
        }
        else
        {
            MsgWarning("联系人已经存在。");
        }
    }
    protected void ucContactor_OnContactorAdded(object sender, EventArgs e)
    {
        DoDataBind();
        ucContactor.Reset();
    }
}
